/**
 * @Project : ProgrammingExercises
 * @Package : Leetcode
 * @File : Q2523.java
 * @Author : WangRuoyu
 * @Date 2023/1/10 16:38
 */

package leetcode;

import java.util.Arrays;

public class Q2523 {
    static boolean isPrime(int x) {
        if (x < 2) {
            return false;
        }

        for (int i = 2; i <= x / i; i++) {
            if (x % i == 0) {
                return false;
            }
        }
        return true;
    }

    public int[] closestPrimes(int left, int right) {
        int[] res = {-1, -1};

        int[] tmp = new int[1000001];
        int count = 0;
        int diff = 1000000;

        for (int i = left; i <= right; i++) {
            if (isPrime(i)) {
                tmp[count] = i;
                count++;
            }
        }

        if (count < 2) {
            return res;
        }

        for (int i = 0; i < count - 1; i++) {
            if (diff > (tmp[i + 1] - tmp[i])) {
                diff = tmp[i + 1] - tmp[i];
                res[0] = tmp[i];
                res[1] = tmp[i + 1];
            }
        }
        return res;
    }

    public static void main(String[] args) {
        Q2523 sl = new Q2523();
        int left = 4;
        int right = 6;
        System.out.println(Arrays.toString(sl.closestPrimes(left, right)));
    }
}